package com.ruoyi.web.controller.note;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.note.service.ITBookKeyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.note.domain.TBookContent;
import com.ruoyi.note.service.ITBookContentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 内容Controller
 *
 * @author cl
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/note/content")
public class TBookContentController extends BaseController {
    @Autowired
    private ITBookContentService tBookContentService;
    @Autowired
    private ITBookKeyService keyService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询内容列表
     */
    @PreAuthorize("@ss.hasPermi('note:content:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBookContent tBookContent, HttpServletRequest request) {
        tBookContent.setUserId(tokenService.getLoginUser(request).getUser().getUserId());

        startPage();
        List<TBookContent> list = tBookContentService.selectTBookContentList(tBookContent);
        return getDataTable(list);
    }

    /**
     * 导出内容列表
     */
    @PreAuthorize("@ss.hasPermi('note:content:export')")
    @Log(title = "内容", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TBookContent tBookContent, HttpServletRequest request) {
        tBookContent.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        List<TBookContent> list = tBookContentService.selectTBookContentList(tBookContent);
        ExcelUtil<TBookContent> util = new ExcelUtil<TBookContent>(TBookContent.class);
        return util.exportExcel(list, "内容数据");
    }

    /**
     * 获取内容详细信息
     */
    @PreAuthorize("@ss.hasPermi('note:content:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TBookContent tBookContent = tBookContentService.selectTBookContentById(id);
        tBookContent.setWentryJson(JSON.toJSONString(this.keyService.selectTBookKeyByContentId(id)));
        return AjaxResult.success(tBookContent);
    }

    /**
     * 新增内容
     */
    @PreAuthorize("@ss.hasPermi('note:content:add')")
    @Log(title = "内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBookContent tBookContent, HttpServletRequest request) {
        tBookContent.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        keyService.handlerByJson(tBookContent, tBookContent.getWentryJson());
        return toAjax(tBookContentService.insertTBookContent(tBookContent));
    }

    /**
     * 修改内容
     */
    @PreAuthorize("@ss.hasPermi('note:content:edit')")
    @Log(title = "内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBookContent tBookContent, HttpServletRequest request) {
        tBookContent.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        keyService.handlerByJson(tBookContent, tBookContent.getWentryJson());
        return toAjax(tBookContentService.updateTBookContent(tBookContent));
    }

    /**
     * 删除内容
     */
    @PreAuthorize("@ss.hasPermi('note:content:remove')")
    @Log(title = "内容", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tBookContentService.deleteTBookContentByIds(ids));
    }
}
